//
//  AppDelegate.m
//  王宇先_01
//
//  Created by wyx on 16/3/9.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MyView.h"
#import "Mybutton.h"

#define W self.window.frame.size.width / 5
#define H self.window.frame.size.height / 8

@interface AppDelegate ()

@end

@implementation AppDelegate
-(void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    /** 主视图 */
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor colorWithRed:128 / 255.f green:128 / 255.f blue:128 / 255.f alpha:1];
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    [_window release];
    [vc release];
#pragma mark - 第一题
#if 0
    //创建 5 个view
    MyView *viewOfRed = [[MyView alloc] initWithFrame:CGRectMake(20, 20, 300, 20) backGroundColor:[UIColor redColor]];
    MyView *viewOfYellow = [[MyView alloc] initWithFrame:CGRectMake(20, 50, 300, 20) backGroundColor:[UIColor yellowColor]];
    MyView *viewOfGreen = [[MyView alloc] initWithFrame:CGRectMake(20, 80, 300, 20) backGroundColor:[UIColor greenColor]];
    MyView *viewOfBlue = [[MyView alloc] initWithFrame:CGRectMake(20, 110, 300, 20) backGroundColor:[UIColor blueColor]];
    MyView *viewOfGray = [[MyView alloc] initWithFrame:CGRectMake(20, 140, 300, 20) backGroundColor:[UIColor grayColor]];
    [self.window addSubview:viewOfRed];
    [self.window addSubview:viewOfYellow];
    [self.window addSubview:viewOfBlue];
    [self.window addSubview:viewOfGreen];
    [self.window addSubview:viewOfGray];
    [viewOfRed release];
    [viewOfYellow release];
    [viewOfBlue release];
    [viewOfGreen release];
    [viewOfGray release];
#endif
#pragma mark - 第二题
#if 0
    //用户名标题
    MyView *viewOfNameEnter = [[MyView alloc] initWithFrame:CGRectMake(40, 80, 90, 40) backGroundColor:[UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1]];
    [self.window addSubview:viewOfNameEnter];
    [viewOfNameEnter release];
    //用户名输入
    MyView *viewOfNameText = [[MyView alloc] initWithFrame:CGRectMake(150, 80, 224, 40) backGroundColor:[UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1]];
    [self.window addSubview:viewOfNameText];
    [viewOfNameText release];
    //密码标题
    MyView *viewOfNameKey = [[MyView alloc] initWithFrame:CGRectMake(40, 130, 90, 40) backGroundColor:[UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1]];
    [self.window addSubview:viewOfNameKey];
    [viewOfNameKey release];
    //密码输入
    MyView *viewOfKeyText = [[MyView alloc] initWithFrame:CGRectMake(150, 130, 224, 40) backGroundColor:[UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1]];
    [self.window addSubview:viewOfKeyText];
    [viewOfKeyText release];
    //登录按钮
    Mybutton *buttonOfEnter = [Mybutton buttonWithType:UIButtonTypeSystem];
    buttonOfEnter.frame = CGRectMake(90, 200, 70, 30);
    buttonOfEnter.backgroundColor = [UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1];
    [self.window addSubview:buttonOfEnter];
    [buttonOfEnter setTitle:@"登录" forState: UIControlStateNormal];
    [buttonOfEnter setTag:100];
    [buttonOfEnter addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside];
    //注册按钮
    Mybutton *buttonOfLogin = [Mybutton buttonWithType:UIButtonTypeSystem];
    buttonOfLogin.frame = CGRectMake(254, 200, 70, 30);
    buttonOfLogin.backgroundColor = [UIColor colorWithRed:0 green:255 / 255.f blue:254 / 255.f alpha:1];
    [self.window addSubview:buttonOfLogin];
    [buttonOfLogin setTitle:@"注册" forState: UIControlStateNormal];
    [buttonOfLogin setTag:200];
    [buttonOfLogin addTarget:self action:@selector(handle:) forControlEvents:UIControlEventTouchUpInside];
#endif
#pragma mark - 第三题
#if 0
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changColor) userInfo:nil repeats:YES];
#endif
#pragma mark - 霓虹灯
#if 1
    MyView *view1 = [[MyView alloc] initWithFrame:CGRectMake(10, 10, 394, 730) backGroundColor:[UIColor redColor]];
    MyView *view2 = [[MyView alloc] initWithFrame:CGRectMake(30, 30, 354, 670) backGroundColor:[UIColor yellowColor]];
    MyView *view3 = [[MyView alloc] initWithFrame:CGRectMake(50, 50, 314, 630) backGroundColor:[UIColor grayColor]];
    MyView *view4 = [[MyView alloc] initWithFrame:CGRectMake(70, 70, 274, 590) backGroundColor:[UIColor blueColor]];
    [self.window addSubview:view1];
    [self.window addSubview:view2];
    [self.window addSubview:view3];
    [self.window addSubview:view4];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeLight) userInfo:nil repeats:YES];
    [view1 release];
    [view2 release];
    [view3 release];
    [view4 release];
#endif
    return YES;
}
//实现第二题handle方法
-(void)handle:(UIButton *)button{
    switch (button.tag) {
        case 100:
            NSLog(@"登陆成功");
            break;
        case 200:
            NSLog(@"请您注册");
        default:
            break;
    }
}
//实现第三题换颜色
- (void)changColor{
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 5; j ++) {
            int a = arc4random() % 255;
            int b = arc4random() % 255;
            int c = arc4random() % 255;
            MyView *view =  [[MyView alloc] initWithFrame:CGRectMake(W * j, H * i, W, H) backGroundColor:[UIColor colorWithRed:a / 255.f green:b / 255.f blue:c / 255.f alpha:1]];
            [self.window addSubview:view];
            [view release];
        }
    }
}
//霓虹灯
- (void)changeLight{
    
    MyView *tempView = [[MyView alloc] init];
//    tempView.backgroundColor = view1.backgroundColor;
//    view1.backgroundColor = view2.backgroundColor;
//    view3.backgroundColor = view4.backgroundColor;
//    view4.backgroundColor = tempView.backgroundColor;
    [tempView release];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
